// shopping_cart.js
var utils = require('../../utils/util.js')
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mode: 'aspectFill',
    goodList:[],
    totalFee:0,
    totalCount:0,
    isAllChecked:false,
    bargainInfo:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this
    app.getUserInfo(function (cb) {
      _this.getPrice()
      _this.getCartList()
      _this.getBargainInfo()
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 选择商品
   */
  checkboxChange: function (e) {
    var checked = e.detail.value
    var changed = {}
    var checkedCount = 0;
    for (var i = 0; i < this.data.goodList.length; i++) {
      if (checked.indexOf(this.data.goodList[i].id) !== -1) {
        changed['goodList[' + i + '].checked'] = true
        checkedCount++
      } else {
        changed['goodList[' + i + '].checked'] = false
        changed['isAllChecked'] = false
      }
    }
    if (this.data.goodList.length == checkedCount) {
      changed['isAllChecked'] = true
    }
    this.setData(changed)
    this.getPrice()
  },

  /**
   * 计算价格
   */
  getPrice:function () {
    var totalCount = 0
    var totalFee = 0
    var changed = {};
    for (var i = 0; i < this.data.goodList.length; i++) {
      var goods = this.data.goodList[i]
      if (goods.checked) {
        totalCount += parseInt(goods.count)
        totalFee += parseFloat(goods.price) * parseFloat(goods.count)
      }
      changed['goodList[' + i + '].fee'] = parseFloat(goods.price) * parseFloat(goods.count)
    }
    changed['totalCount'] = totalCount
    changed['totalFee'] = totalFee
    this.setData(changed)
  },

  /**
   * 减商品
   */
  decCount:function (e) {
    var index = e.target.dataset.index
    var id = e.target.dataset.id
    var _this = this
    var changed = {}
    var count = parseInt(_this.data.goodList[index].count)
    if (parseInt(count) > 1) {
      count--
      changed['goodList[' + index + '].count'] = count
      _this.setData(changed)
      _this.getPrice()
      wx.request({
        url: app.globalData.requestip + "&c=Cart&a=setDecCount",
        data: {
          id: id
        },
        header: app.globalData.header,
        success: function (res) {
          if (res.data.code == 0) {
          } else {
            utils.loadFail(res)
          }
        },
        fail: function (res) {
          utils.loadRequestFail(res)
        }
      })
    }
  },

  /**
   * 加商品
   */
  incCount: function (e) {
    var index = e.target.dataset.index
    var id = e.target.dataset.id
    var _this = this
    var changed = {}
    var count = parseInt(_this.data.goodList[index].count)
    if (parseInt(count) >= 1) {
      count++
      changed['goodList[' + index + '].count'] = count
      _this.setData(changed)
      _this.getPrice()
      wx.request({
        url: app.globalData.requestip + "&c=Cart&a=setIncCount",
        data: {
          id: id
        },
        header: app.globalData.header,
        success: function (res) {
          if (res.data.code == 0) {
          } else {
            utils.loadFail(res)
          }
        },
        fail: function (res) {
          utils.loadRequestFail(res)
        }
      })
    }
  },

  /**
   * 全选切换
   */
  checkAll: function () {
    var changed = {}
    if (this.data.isAllChecked) {
      changed['isAllChecked'] = false
      for (var i = 0; i < this.data.goodList.length; i++) {
        changed['goodList[' + i + '].checked'] = false
      }
    } else {
      changed['isAllChecked'] = true
      for (var i = 0; i < this.data.goodList.length; i++) {
        changed['goodList[' + i + '].checked'] = true
      }
    }
    this.setData(changed)
    this.getPrice()
  },

  /**
   * 删除购物车
   */
  removeCart: function (e) {
    var id = e.target.dataset.id
    var _this = this
    wx.showModal({
      content: '确认删除',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.requestip + "&c=Cart&a=removeCart",
            data:{
              id:id
            },
            header: app.globalData.header,
            success: function (res) {
              if (res.data.code == 0) {
                _this.getCartList()
              } else {
                utils.loadFail(res)
              }
            },
            fail: function (res) {
              utils.loadRequestFail(res)
            }
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  /**
   * 获取购物车列表
   */
  getCartList:function () {
    var _this = this
    wx.request({
      url: app.globalData.requestip + "&c=Cart&a=getCartList",
      header: app.globalData.header,
      success: function (res) {
        if (res.data.code == 0) {
          _this.setData({
            goodList:res.data.list
          })
        } else {
          utils.loadFail(res)
        }
      },
      fail: function (res) {
        utils.loadRequestFail(res)
      }
    })
  },

  /**
   * 获取砍价信息
   */
  getBargainInfo: function () {
    var _this = this
    wx.request({
      url: app.globalData.requestip + "&c=Cart&a=getCurrentBargain",
      header: app.globalData.header,
      success: function (res) {
        if (res.data.code == 0) {
          _this.setData({
            bargainInfo: res.data.data
          })
        } else {
          utils.loadFail(res)
        }
      },
      fail: function (res) {
        utils.loadRequestFail(res)
      }
    })
  },

  /**
   * 点击结算
   */
  tapBuy:function(e) {
    var _this = this
    var goodsList = _this.data.goodList
    var checkedList = []
    for (var i = 0; i < goodsList.length; i++) {
      if (goodsList[i].checked) {
        checkedList.push(goodsList[i].id)
      }
    }
    if (checkedList.length == 0) {
      wx.showToast({
        title: '请选择商品',
        duration: 1000,
        mask: true,
      })
    } else {
      wx.request({
        url: app.globalData.requestip + "&c=Order&a=doAddOrder",
        data:{
          id_list: checkedList
        },
        header: app.globalData.header,
        success: function (res) {
          if (res.data.code == 0) {
            wx.navigateTo({
              url: '../confirm_buy/confirm_buy?order_no=' + res.data.order_no,
            })
          } else {
            utils.loadFail(res)
          }
        },
        fail: function (res) {
          utils.loadRequestFail(res)
        }
      })
    }
  },

  tapBargain: function () {
    var _this = this
    wx.navigateTo({
      url: '../price/price?order_no='+_this.data.bargainInfo.order_no,
    })
  }

})